﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using lumo.input;
using lumo.display.object2D;

namespace lumo.display.gui
{
    /// <summary>
    /// Menu class.
    /// </summary>
    public class Menu : IObject2D, IUpdatableOnce
    {
        // Internal command list.
        private ICommandList _CommandList = null;
        private Dictionary<int, List<Action<Menu>>> ActionDictionary = new Dictionary<int, List<Action<Menu>>>();

        /// <summary>Input variable.</summary>
        public IInputObject Input;

        /// <summary>
        /// Command List accessor for menu.
        /// </summary>
        public ICommandList CommandList
        {
            get
            {
                return _CommandList;
            }
            set
            {
                ActionDictionary.Clear();
                if (value != null)
                {
                    for (int i = 0; i < value.Count; ++i)
                        ActionDictionary[i] = new List<Action<Menu>>();
                }
                _CommandList = value;
            }
        }

        /// <summary>
        /// Add an action for an index.
        /// </summary>
        /// <param name="Index">The index.</param>
        /// <param name="IndexAction">The action.</param>
        public void OnIndex(int Index, Action<Menu> IndexAction)
        {
            List<Action<Menu>> CurrentList = null;
            if (ActionDictionary.TryGetValue(Index, out CurrentList))
                CurrentList.Add(IndexAction);
        }

        #region IUpdatable

        /// <summary>
        /// Frame id
        /// </summary>
        public uint FrameID { get; private set; }

        /// <summary>
        /// Update the menu.
        /// </summary>
        public void Update(LumoComponent Game)
        {
            this.FrameID = Game.FrameID;
            if (Input != null)
                Input.Update(Game);
            if (_CommandList != null)
            {
                _CommandList.Update(Game);
                if (_CommandList.Active)
                {
                    if ((Input != null) && Input.Triggered)
                    {
                        List<Action<Menu>> CurrentList = null;
                        if (ActionDictionary.TryGetValue(_CommandList.Index, out CurrentList))
                        {
                            foreach (Action<Menu> Action in CurrentList)
                                Action(this);
                        }
                    }
                }
            }
        }

        #endregion

        #region IObejct2D

        /// <summary>
        /// Draw implementaion of IObject2D
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            if (_Visible && (_CommandList != null))
                _CommandList.Draw(spriteBatch);
        }

        /// <summary>Private draw order float.</summary>
        private float _DrawOrder = 0f;
        /// <summary>
        /// Simple implementation of draw order.
        /// </summary>
        public float DrawOrder { get { return _DrawOrder; } set { _DrawOrder = value; } }

        /// <summary>Private visible variable.</summary>
        private bool _Visible = true;        
        /// <summary>
        /// Simple implementation of Visible.
        /// </summary>
        public bool Visible { get { return _Visible; } set { _Visible = value; } }

        #endregion
    }
}
